import TestCase.AssociatedServicesTestCase;
import TestCase.FileServicesTestCase;
import TestCase.Test_Case_0;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.CheckUtil;
import util.StaticMessageUtil;


public class IEC61850AutoTestApp {
    private static final Logger logger = LoggerFactory.getLogger("IEC61850-Client");

    private static Map<String, String> caseList = new LinkedHashMap<String,String>();
    private static Map<String, String> resultsList = new LinkedHashMap<String,String>();
    private static AssociatedServicesTestCase associatedServicesTestCase = new AssociatedServicesTestCase();
    private static FileServicesTestCase fileServicesTestCase = new FileServicesTestCase();
    private static void addTestCaseList(){
        caseList.put("1","建立关联和释放关联250次测试");
        caseList.put("2","建立关联和中止关联250次测试");
        caseList.put("3","最大客户端为16个");
        caseList.put("4","获取存在的目录或文件属性");
        caseList.put("5","获取不存在的目录或文件属性");
        caseList.put("0","完成所有用例测试，测试过程中必须的参数将采用默认参数");
        caseList.put("h","帮助信息");
        caseList.put("q","退出测试程序");


    }

    public static void main(String[] args) throws InterruptedException {


        String caseId = null;
        String serverIp = null;
        addTestCaseList();
        CheckUtil checkUtil = new CheckUtil();

        StaticMessageUtil staticMessageUtil = new StaticMessageUtil();
        staticMessageUtil.welcome();


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String scane = null;
        logger.debug("IEC61850 Auto Test Program Started !");
        while (true){
            while (true){
                staticMessageUtil.helpMessage(caseList);
                System.out.print("The Test Case you want choose: ");
                try {
                    scane = br.readLine();
                } catch (IOException e) {
                    logger.error("The Test Case input error. Reason: " + e.getMessage());
                }
                if (checkUtil.testCaseIdCheck(scane)){
                    caseId = scane;
                    break;
                } else {
                    logger.error("The Test Case input error. Reason: " + scane);
                }
            }
            if ("h".equals(caseId)){
                continue;
            }
            if ("q".equals(caseId)){
                break;
            }
            System.out.print("The IP/domain address of the IEC61850 server you want to access:");
            try {
                scane = br.readLine();
            } catch (IOException e) {
                logger.error("IEC61850 server address input error. Reason: " + e.getMessage());
            }
            if (checkUtil.ipCheck(scane)) {
                serverIp = scane;
            } else {
                logger.error("IP Format Is Error. Reason: " + scane);
            }
            boolean flag = false;

            switch (caseId){
                case "0":{
                    if (associatedServicesTestCase.AssociationsWereEstablishedAndReleased_250_timesTest(serverIp)){
                        resultsList.put("1","PASS");
                    } else {
                        resultsList.put("1","FAIL");
                    }
                    Thread.currentThread().sleep(3000);
                    if( associatedServicesTestCase.AssociationsWereEstablishedAndAbort_250_timesTest(serverIp)){
                        resultsList.put("2","PASS");
                    } else {
                        resultsList.put("2","FAIL");
                    }
                    Thread.currentThread().sleep(3000);
                    if(associatedServicesTestCase.MaxClientTest(serverIp)){
                        resultsList.put("3","PASS");
                    } else {
                        resultsList.put("3","FAIL");
                    }
                    Thread.currentThread().sleep(3000);
                    if(fileServicesTestCase.GetExistsFileAttributes(serverIp)){
                        resultsList.put("4","PASS");
                    } else {
                        resultsList.put("4","FAIL");
                    }
                    Thread.currentThread().sleep(3000);
                    if(fileServicesTestCase.GetNoneExistsFileAttributes(serverIp)){
                        resultsList.put("5","PASS");
                    } else {
                        resultsList.put("5","FAIL");
                    }
                    break;
                }
                case "1":{
                    flag = associatedServicesTestCase.AssociationsWereEstablishedAndReleased_250_timesTest(serverIp);
                    break;
                }
                case "2":{
                    flag = associatedServicesTestCase.AssociationsWereEstablishedAndAbort_250_timesTest(serverIp);
                    break;
                }
                case "3":{
                    flag = associatedServicesTestCase.MaxClientTest(serverIp);
                    break;
                }
                case "4":{
                    flag = fileServicesTestCase.GetExistsFileAttributes(serverIp);
                    break;
                }
                case "5":{
                    flag = fileServicesTestCase.GetNoneExistsFileAttributes(serverIp);
                    break;
                }
                default:break;
            }
            if (!"0".equals(caseId)){
                if (flag){
                    resultsList.put(caseId,"PASS");
                } else {
                    resultsList.put(caseId,"FAIL");
                }
            }

            staticMessageUtil.ResultPrint(caseList, resultsList);
            resultsList.clear();
        }
        logger.debug("IEC61850 Auto Test Program Stopped !");
    }
}
